#pragma once
#include <atomic>
#include <string>

namespace zh
{
    class Buffer
    {
    public:
        Buffer()  = default;
        ~Buffer() = default;

        void initialize();

        size_t readableBytes() const;
        size_t writeableBytes() const;

        ssize_t readFD(int FD, int* saveErroNo);
        ssize_t writeFD(int FD, int* saveErroNo);

        void append(const char* data, size_t length);
        void append(const std::string& data);

        const char* data() const;
        size_t      length() const;
        void        reset();

        std::string toString() const;

        const char* curReadPtr() const;
        const char* curWritePtr() const;

        void updateReadPtr(int length);
        void updateWritePtr(int length);

    private:
        char*       beginPtr();
        const char* beginPtr() const;

    private:
        std::string m_buffer;

        int                      m_initSize {1024};
        std::atomic<std::size_t> m_readPosition {0};
        std::atomic<std::size_t> m_writePosition {0};
    };
} // namespace zh